package ceha;

import addons.MysqlConnect;

import cehadata.DataFrame;
import cehadata.FicheMasterDetailForm;
import cehadata.FicheFrame;
import java.awt.Color;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class CehaApp extends javax.swing.JFrame {

    public static String REPIMAGES = "C:/IMAGES/";

    public CehaApp() {
        //dossier des images 

        try {
            // "com.sun.java.swing.plaf.motif.MotifLookAndFeel"
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
// UI modifiable    
// new JLookAndFeelChooser(this).setVisible(true);
        initComponents();
//        Picture logo = new Picture("LogoIdf_Tour_Eiffel.jpg");
//        this.mainpanel.add(logo);
    }

  

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainpanel = new javax.swing.JPanel();
        connectPanel = new javax.swing.JPanel();
        pseudoField = new javax.swing.JTextField();
        passField = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        ValideButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        commadePanel = new javax.swing.JPanel();
        recherchButton = new javax.swing.JButton();
        passePanel = new javax.swing.JPanel();
        adminButton = new javax.swing.JButton();
        indexButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        quitterButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(255, 255, 255));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(java.awt.Color.white);
        setIconImages(null);
        setResizable(false);

        mainpanel.setBackground(new java.awt.Color(51, 51, 51));
        mainpanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        connectPanel.setBackground(new java.awt.Color(255, 204, 0));

        pseudoField.setBackground(new java.awt.Color(255, 255, 204));
        pseudoField.setFont(new java.awt.Font("Tahoma", 1, 12));
        pseudoField.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pseudoField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pseudoFieldActionPerformed(evt);
            }
        });

        passField.setBackground(new java.awt.Color(255, 255, 204));
        passField.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel1.setText("Pseudo :");

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel2.setText("Mot de passe :");

        ValideButton.setBackground(new java.awt.Color(255, 255, 255));
        ValideButton.setFont(new java.awt.Font("Times New Roman", 1, 14));
        ValideButton.setText("Valider");
        ValideButton.setToolTipText("cliquez pour valider");
        ValideButton.setOpaque(false);
        ValideButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ValideButtonActionPerformed(evt);
            }
        });

        jButton1.setBackground(new java.awt.Color(255, 255, 255));
        jButton1.setFont(new java.awt.Font("Times New Roman", 1, 14));
        jButton1.setText("Annuler");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout connectPanelLayout = new javax.swing.GroupLayout(connectPanel);
        connectPanel.setLayout(connectPanelLayout);
        connectPanelLayout.setHorizontalGroup(
            connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(connectPanelLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(connectPanelLayout.createSequentialGroup()
                        .addGroup(connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pseudoField, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                            .addComponent(passField, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, connectPanelLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(ValideButton, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addGap(32, 32, 32))))
        );
        connectPanelLayout.setVerticalGroup(
            connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(connectPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pseudoField, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passField, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(connectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ValideButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        commadePanel.setBackground(new java.awt.Color(255, 255, 255));

        recherchButton.setFont(new java.awt.Font("Times New Roman", 1, 24));
        recherchButton.setForeground(new java.awt.Color(0, 0, 204));
        recherchButton.setText("Recherches");
        recherchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                recherchButtonActionPerformed(evt);
            }
        });

        passePanel.setBackground(new java.awt.Color(255, 204, 0));

        adminButton.setBackground(new java.awt.Color(51, 51, 51));
        adminButton.setFont(new java.awt.Font("Times New Roman", 1, 18));
        adminButton.setForeground(new java.awt.Color(153, 0, 0));
        adminButton.setText("Administration");
        adminButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AdminActionPerformed(evt);
            }
        });

        indexButton.setBackground(new java.awt.Color(0, 0, 0));
        indexButton.setFont(new java.awt.Font("Times New Roman", 1, 18));
        indexButton.setForeground(new java.awt.Color(153, 0, 0));
        indexButton.setText("Indexation");
        indexButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FicheActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout passePanelLayout = new javax.swing.GroupLayout(passePanel);
        passePanel.setLayout(passePanelLayout);
        passePanelLayout.setHorizontalGroup(
            passePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(passePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(passePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(indexButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                    .addComponent(adminButton, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        passePanelLayout.setVerticalGroup(
            passePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, passePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(indexButton, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
                .addComponent(adminButton)
                .addContainerGap())
        );

        javax.swing.GroupLayout commadePanelLayout = new javax.swing.GroupLayout(commadePanel);
        commadePanel.setLayout(commadePanelLayout);
        commadePanelLayout.setHorizontalGroup(
            commadePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(commadePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(recherchButton)
                .addGap(18, 18, 18)
                .addComponent(passePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(53, Short.MAX_VALUE))
        );
        commadePanelLayout.setVerticalGroup(
            commadePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(commadePanelLayout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(recherchButton)
                .addContainerGap(79, Short.MAX_VALUE))
            .addGroup(commadePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(passePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(12, 12, 12))
        );

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 24));
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("BASE DE DONNEES DU CEHA");

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ceha/ressource/logoCEHA.jpg"))); // NOI18N

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ceha/ressource/EcussonPOISSY.jpg"))); // NOI18N

        quitterButton.setForeground(new java.awt.Color(255, 255, 255));
        quitterButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ceha/ressource/Quitter.jpg"))); // NOI18N
        quitterButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        quitterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitterButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout mainpanelLayout = new javax.swing.GroupLayout(mainpanel);
        mainpanel.setLayout(mainpanelLayout);
        mainpanelLayout.setHorizontalGroup(
            mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainpanelLayout.createSequentialGroup()
                .addGroup(mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainpanelLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(commadePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(77, 77, 77))
                    .addGroup(mainpanelLayout.createSequentialGroup()
                        .addContainerGap(130, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addGap(29, 29, 29))
                    .addGroup(mainpanelLayout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(connectPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(mainpanelLayout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(37, 37, 37)
                        .addComponent(quitterButton, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        mainpanelLayout.setVerticalGroup(
            mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainpanelLayout.createSequentialGroup()
                .addGroup(mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainpanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(commadePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(connectPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainpanelLayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainpanelLayout.createSequentialGroup()
                                .addComponent(quitterButton)
                                .addGap(35, 35, 35)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainpanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainpanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-776)/2, (screenSize.height-427)/2, 776, 427);
    }// </editor-fold>//GEN-END:initComponents

    private void pseudoFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pseudoFieldActionPerformed
    }//GEN-LAST:event_pseudoFieldActionPerformed

    private void FicheActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FicheActionPerformed
        //nouvelle fiche
      EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                FicheFrame frame = new FicheFrame();
                frame.getFiche().setContentPane(new FicheMasterDetailForm());              
                frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
       
    }//GEN-LAST:event_FicheActionPerformed

    private void AdminActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AdminActionPerformed
        connectPanel.setBackground(Color.red);

    }//GEN-LAST:event_AdminActionPerformed

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
        pseudoField.setText("");
        passField.setText("");
    }//GEN-LAST:event_CancelActionPerformed

    private void ValideButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ValideButtonActionPerformed
        Connection conn = new MysqlConnect().getConn();
        String nom = pseudoField.getText();
        String mot = passField.getPassword().toString();
        Statement stmt;
        ResultSet res = null;
        List<String> Lpseudo = new ArrayList<String>();
        List<String> Lpass = new ArrayList<String>();
        try {
            stmt = conn.createStatement();
            res = stmt.executeQuery("SELECT * FROM membre");
            while (res.next()) {
                Lpseudo.add(res.getString("pseudo"));
                Lpass.add(res.getString("pass"));
            }
            res.close();
            stmt.close();
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        int no = 0;
        while (no < Lpseudo.size()) {
            if ((Lpseudo.get(no).equals(nom)) && (Lpass.get(no).equals(mot))) {
                connectPanel.setBackground(Color.green);
                break;
            } else {
                no++;
            }
            connectPanel.setBackground(Color.red);
        }
    }//GEN-LAST:event_ValideButtonActionPerformed

    private void quitterButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitterButtonActionPerformed
       System.exit(0);
    }//GEN-LAST:event_quitterButtonActionPerformed

    private void recherchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_recherchButtonActionPerformed
            //nouvelle fiche
      EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                DataFrame frame = new DataFrame();
                FicheMasterDetailForm FMD = new FicheMasterDetailForm();
                FMD.getFichepanel().setVisible(false);
                frame.getData().setContentPane(FMD);
                frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }//GEN-LAST:event_recherchButtonActionPerformed

    public static void main(String args[]) {
        new CehaApp().setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ValideButton;
    private javax.swing.JButton adminButton;
    private javax.swing.JPanel commadePanel;
    private javax.swing.JPanel connectPanel;
    private javax.swing.JButton indexButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel mainpanel;
    private javax.swing.JPasswordField passField;
    private javax.swing.JPanel passePanel;
    private javax.swing.JTextField pseudoField;
    private javax.swing.JButton quitterButton;
    private javax.swing.JButton recherchButton;
    // End of variables declaration//GEN-END:variables
}
